﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using CeA.Reports.Atendimento.Web.ServerControls;
using System.Collections.Generic;
using CeA.Reports.Atendimento.Web.Helpers;
using CeA.Reports.Atendimento.DataAccess.DTO;
using Qtech.Reports.Atendimento.Services;
using CeA.Reports.Atendimento.DataAccess.Criteria;

namespace CeA.Reports.Atendimento.Web.WebControls {
    public partial class PesquisaRoles : System.Web.UI.UserControl {

        #region -- Event Model ----------------------------------------------------

        // Event Keys
        private static readonly object ConfirmarEventKey = new object();


        // Delegates
        public delegate void ConfirmarEventHandler(object sender, PesquisaRolesEventArgs e);


        // Events
        public event ConfirmarEventHandler Confirmar {
            add {
                Events.AddHandler(ConfirmarEventKey, value);
            }
            remove {
                Events.RemoveHandler(ConfirmarEventKey, value);
            }
        }


        protected virtual void OnConfirmar(PesquisaRolesEventArgs e) {
            ConfirmarEventHandler handler = Events[ConfirmarEventKey] as ConfirmarEventHandler;
            if (handler != null) {
                handler(this, e);
            }
        }
        #endregion


        protected void Page_Init(object sender, EventArgs e)            {
            cmdConfirmar.Click += new EventHandler(cmdConfirmar_Click);
            cmdCancelar.Click += new EventHandler(cmdCancelar_Click);
        }


        protected void Page_Load(object sender, EventArgs e)            {
            if (!Page.IsPostBack) {
                popup.Visible = false;
                carregar();
            }
        }


        protected void cmdPesquisar_Click(object sender, EventArgs e)   {
            
        }


        protected void cmdConfirmar_Click(object sender, EventArgs e)   {
            PesquisaRolesEventArgs args = new PesquisaRolesEventArgs();

            foreach (Control ctl in dataList.Items) {
                Control c = ctl.FindControl("chkItem");
                if (c is CheckBox) {
                    CheckBox chk = (c as CheckBox);
                    if (chk.Checked) {
                        RoleDTO role = new RoleDTO();
                        role.Id = Convert.ToInt32((c.FindControl("chkItemId") as HiddenField).Value);
                        role.Nome = (c.FindControl("lblNome") as Label).Text;
                        role.Descricao = (c.FindControl("lblDescricao") as Label).Text;


                        args.addRole(role);
                    }
                }
            }

            popup.Visible = false;

            if(args != null) OnConfirmar(args);
        }


        protected void cmdCancelar_Click(object sender, EventArgs e)    {
            popup.Visible = false;
        }


        public void pop()                                               {
            popup.Visible = true;
        }


        public void markSelected(List<RoleDTO> roles)                   {
            if (roles.Count == 0) return;

            foreach (Control ctl in dataList.Items) {
                Control c = ctl.FindControl("chkItem");
                if (c is CheckBox) {
                    CheckBox chk = (c as CheckBox);
                    int actualId = Convert.ToInt32((c.FindControl("chkItemId") as HiddenField).Value);
                    roles.Find(
                        delegate(RoleDTO role) {
                            if (role.Id == actualId) {
                                chk.Checked = true;
                                return true;
                            }
                            else {
                                return false;
                            }
                        });
                }
            }
        }


        private void carregar()                                         {
            List<RoleDTO> ds = CadastroFacade.FindRoles();
            if (ds.Count > 0) cmdConfirmar.Enabled = true;

            dataList.DataSource = ds;
            dataList.DataBind();
        }
    }
}